#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#include "outputAPI.h"

//#define F_OFF _off64_t
#ifdef WINDOWS
    /*#define F_OFF off64_t      */
	#ifdef __MINGW32__
		#define F_OFF _off64_t
	#else
    	#define F_OFF __int64
	#endif
#else
    #define F_OFF off_t
#endif
// define fseeko64 in different platform
#ifdef WINDOWS
    #ifdef __MINGW32__
       int (*fseeko64_)(FILE*, F_OFF, int) = &fseeko64;
    #else
        int (*fseeko64_)(FILE*, F_OFF, int) = &_fseeki64;
    #endif
#else
     int (*fseeko64_)(FILE*, F_OFF, int) = &fseeko;
#endif

int main(int argc, char* argv[]){
	
	char path[MAXFILENAME];
	strcpy(path, argv[1]);
	printf("test output file path: %s\n",path);

	// 初始化文件指针
	SMOutputAPI* smoapi = (SMOutputAPI*)malloc(sizeof(SMOutputAPI)); 
	// 导入库文件
	HMODULE module = LoadLibrary("libswmmoutputapi.dll");
	if (module == NULL){
		printf("Failed to load dll\n");
	}
	
	// 定义SMO_open函数，并指向dll中的对应函数
	typedef int(*openFun)(SMOutputAPI*, const char*);
	openFun SMO_open = (openFun)GetProcAddress(module, "SMO_open");
	// 定义SMO_getProjectSize函数，并指向dll中的对应函数
	typedef int(*getPrjSize)(SMOutputAPI* smoapi, SMO_elementCount code, int* count);
	getPrjSize SMO_getProjectSize = (getPrjSize)GetProcAddress(module, "SMO_getProjectSize");
	//
	// 打开输出文件
	// --- open the output file
	int errBEM = SMO_open(smoapi, path);
	printf("err code: %i\n", errBEM);
	int count, count2, count3;
	// get subcatch, node, link counts
	SMO_getProjectSize(smoapi, subcatchCount, &count);
	printf("Subcatch Count: %i\n", count);
	SMO_getProjectSize(smoapi, nodeCount, &count2); // 12 junctions + 1 outfall + 1 storage
	printf("Node Count: %i\n", count2);	
	SMO_getProjectSize(smoapi, linkCount, &count3); // 12 conduits + 3 orifices + 1 weir
	printf("Link Count: %i\n", count3);	
	FreeLibrary(module);

	return 0;
}
